<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2019/1/25
 * Time: 17:01
 */

namespace app\sports_admin\controller;
use think\Request;
use app\sports_admin\model\SportProject as SportProjectModel;
use app\exception\SportProjectException;
use app\sports_admin\service\SportProject as SportProjectService;


class SportProject extends Base
{
    /**
     * 运动项目类型页面
     */
    public function sportProjectIndex()
    {
        return view('index');
    }

    /**
     * 运动项目类型列表
     */
    public function sportProjectList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        return SportProjectModel::sportProjects($page,$rows,$sort,$sortOrder);
    }
    /**
     * 添加运动类型
     */
    public function sportProjectAdd(Request $request)
    {
        if($request->isAjax()){
            $data = SportProjectService::sportProjectAdd();
            return $data;
        }
    }
    /*
     * 编辑运动项目
     * @param $id 项目类型id
     */
    public function edit()
    {
        $id = intval(input('get.id'));
        $info = SportProjectModel::where('id',$id)->find();
        return view('edit',compact('info'));
    }
    /*
     * 修改运动项目名称
     */
    public function update(Request $request)
    {
        if($request->isAjax()){
            return SportProjectService::update();
        }
    }
    /**
     * 删除运动类型
     */
    public function sportProjectDel(Request $request)
    {
        if($request->isAjax()){
            $data = SportProjectModel::sportProjectDel();
            if($data){
                return json(['code'=>200,'msg'=>"删除成功"]);
            }
            throw new SportProjectException(['errorCode'=>400,'msg'=>"删除失败"]);
        }
    }

}